package metamodel.coreservices;

import metamodel.transformer.BottomUpGraphTransformer;
import metamodel.transformer.PropertyValue;
import metamodel.Property;
import metamodel.ClassNode;
import metamodel.CollectionRef;
import metamodel.FlatProperty;

import java.util.List;
import java.util.Collection;

/**
 * User: fleipold
 * Date: 12-Dec-2007
 * Time: 18:21:09
 */
public class CloneTransformer implements BottomUpGraphTransformer {
    public Object mapObject(ClassNode meta, List<PropertyValue> mappedProperties, Object originalObject) {
        try {
            Object newObject = meta.getType().newInstance();
            for (PropertyValue value : mappedProperties) {
                value.getMeta().setValue(newObject, value.getValue());
            }
            return newObject;
        } catch (InstantiationException e) {
            throw new RuntimeException(e);
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        }
    }

    public Object mapCollection(CollectionRef meta, Collection mappedElements, Collection originalCollection) {
        return mappedElements;
    }

    public Object mapFlatProperty(FlatProperty meta, Object value) {
        return value;
    }

    public void mapCycle(Property meta, Object mappedSource, Object orginalSource, Object mappedDestination, Object originalDestination) {
        meta.setValue(mappedSource,mappedDestination);
}
}
